package main

import (
	"fmt"
	"time"
)

func unixToTime(timestamp int64) {
	timeObj := time.Unix(timestamp, 0) //将时间戳转为时间格式
	year := timeObj.Year()             //年
	month := timeObj.Month()           //月
	day := timeObj.Day()               //日
	hour := timeObj.Hour()             //小时
	minute := timeObj.Minute()         //分钟
	second := timeObj.Second()         //秒
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
}

func main() {

	unixToTime(1587880013)

	var timestamp int64 = 1587880013 //时间戳
	t := time.Unix(timestamp, 0)
	fmt.Println(t.Format("2006-01-02 15:04:05"))

	t1 := "2019-01-08 13:50:30" //时间字符串
	timeTemplate := "2006-01-02 15:04:05"
	stamp, _ := time.ParseInLocation(timeTemplate, t1, time.Local)
	fmt.Println(stamp.Unix())
}
